package com.dss.auto_deploy.util.mail;

import com.dss.auto_deploy.config.DssProperties;
import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

/**
 * @description: 邮件跑龙套
 * @author: dongshoushan
 * @date:2022-11-23 20:55:59
 */
@Component
public class MailUtil {
    @Resource
    private DssProperties dssProperties;

    @Bean
    private JavaMailSenderImpl mailSender(){
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(dssProperties.getEmail().getHost());
        javaMailSender.setUsername(dssProperties.getEmail().getAccount());
        javaMailSender.setPassword(dssProperties.getEmail().getPassword());
        Properties properties = new Properties();
        properties.put("mail.smtp.auth",dssProperties.getEmail().getIsAuth());
        properties.put("mail.smtp.timeout",dssProperties.getEmail().getOutTime());
        javaMailSender.setJavaMailProperties(properties);
        return javaMailSender;
    }

    public void sendSimpleMail(MyEmail myEmail) throws  UnsupportedEncodingException {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        String nick = javax.mail.internet.MimeUtility.encodeText("跨三论剑会长");
        // 设置发件人邮箱地址
        mailMessage.setFrom((nick + " <" + dssProperties.getEmail().getAccount() + ">"));
        String receiver = myEmail.getReceiver();
        String[] split = receiver.split(",");
        mailMessage.setTo(split);
        mailMessage.setSubject(myEmail.getSubject());
        mailMessage.setText(myEmail.getText());
        mailSender().send(mailMessage);
    }

    public void sendMail(MyEmail email) throws MessagingException, UnsupportedEncodingException, GeneralSecurityException {
        Properties properties = new Properties();
        properties.put("mail.debug", true);
        properties.put("mail.smtp.host", dssProperties.getEmail().getHost());
        properties.put("mail.smtp.port", dssProperties.getEmail().getPort());
        properties.put("mail.smtp.auth", true);
        //office365的配置
//        properties.put("mail.smtp.starttls.enable", "true");
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.socketFactory", sf);
        // 设置是否使用ssl安全连接 ---一般都使用
        properties.put("mail.smtp.ssl.enable", "true");
        // 得到回话对象
        Session session = Session.getDefaultInstance(properties);
        session.setDebug(true);

        String recipients = email.getReceiver();

        // 获取邮件对象
        Message message = new MimeMessage(session);
        String nick = javax.mail.internet.MimeUtility.encodeText("发布平台");
        // 设置发件人邮箱地址
        message.setFrom(new InternetAddress(nick +  " <" +dssProperties.getEmail().getAccount() + ">"));
        // 设置收件人邮箱地址
        String[] recipientsArr = recipients.split(",");
        InternetAddress[] internetAddresses = new InternetAddress[recipientsArr.length];
        for (int i = 0; i < recipientsArr.length; i++) {
            internetAddresses[i] = new InternetAddress(recipientsArr[i]);
        }
        message.setRecipients(Message.RecipientType.TO, internetAddresses);

        // 设置邮件标题
        message.setSubject(email.getSubject());
        // 邮件内容,也可以使纯文本"text/plain"
        // 创建一个多用途互联网邮件扩展（MIME）多部分消息
        Multipart multiPart = new MimeMultipart();
        // 添加普通文本部分
        MimeBodyPart textBodyPart = new MimeBodyPart();
        textBodyPart.setText(email.getText(),  "utf-8", "html");
        multiPart.addBodyPart(textBodyPart);
        // 将多部分消息设置为内容
        message.setContent(multiPart);
        //message.setContent(email.getText(),"text/html;charset=UTF-8");
        // 设置显示的发件时间
        message.setSentDate(new Date());
        //保存邮件
        message.saveChanges();
        //发送邮件
        Transport transport = session.getTransport("smtp");
        //连接服务器的邮箱
        transport.connect(dssProperties.getEmail().getHost(), dssProperties.getEmail().getAccount(), dssProperties.getEmail().getPassword());
        //把邮件发送出去
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();//关闭连接

    }
}
